package com.justgame.project.service.system.service.impl;

import com.justgame.project.base.servcice.BasicServiceImpl;
import com.justgame.project.cloud.base.entity.CurrentUser;
import com.justgame.project.cloud.base.entity.FileSizeUnit;
import com.justgame.project.cloud.base.entity.OssFile;
import com.justgame.project.cloud.common.enums.FileTypeEnum;
import com.justgame.project.cloud.common.util.FileUtils;
import com.justgame.project.service.system.entity.SysUserInfo;
import com.justgame.project.service.system.entity.SysUserOperationLog;
import com.justgame.project.service.system.entity.SysUserSocialInfo;
import com.justgame.project.service.system.mapper.SysUserSocialInfoMapper;
import com.justgame.project.service.system.service.ISysUserOperationLogService;
import com.justgame.project.service.system.service.ISysUserSocialInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.time.Duration;

/**
 *
 * 用户社交信息表服务实现类
 *
 * @@tableName sys_user_social_info
 * @author Zwb
 */
@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class SysUserSocialInfoServiceImpl extends BasicServiceImpl<SysUserSocialInfoMapper, SysUserSocialInfo> implements ISysUserSocialInfoService {
    private final ISysUserOperationLogService sysUserOperationLogService;

    @Override
    @Transactional
    public String updateAvatar(@NonNull CurrentUser currentUser, @NonNull MultipartFile file) {
        sysUserOperationLogService.doOperation(currentUser, SysUserOperationLog.Operation.UPDATE_AVATAR, Duration.ofDays(1));
        OssFile ossFile = FileUtils.upload(file, FileTypeEnum.Image, FileSizeUnit.MEGABYTES, 10, false);
        Assert.hasLength(ossFile.getPath());
        SysUserSocialInfo userSocialInfo = findByUserInfoIdIs(currentUser.getId()).orElseThrow();
        updateByAvatar(userSocialInfo,ossFile.getPath());
        return ossFile.getPath();
    }
}
